/*
 * Copyright© 2003-2019 浙江汇信科技有限公司, All Rights Reserved.
 */
package com.icinfo.cloud.provider.punish.ucase.casemain.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.icinfo.cloud.provider.common.constant.Constant;
import com.icinfo.cloud.provider.common.enums.*;
import com.icinfo.cloud.provider.common.exception.BusinessException;
import com.icinfo.cloud.provider.common.utils.BaseUtil;
import com.icinfo.cloud.provider.common.utils.DateUtil;
import com.icinfo.cloud.provider.punish.common.documentdataload.dto.CasePartiesQueryDto;
import com.icinfo.cloud.provider.punish.config.model.ConfigBusiParamDocumentInfo;
import com.icinfo.cloud.provider.punish.config.service.IConfigBusiParamDocumentInfoService;
import com.icinfo.cloud.provider.punish.csource.manage.mapper.CaseSourcePartyInfoMapper;
import com.icinfo.cloud.provider.punish.csource.manage.model.CaseSourcePartyInfo;
import com.icinfo.cloud.provider.punish.ucase.casemain.dto.*;
import com.icinfo.cloud.provider.punish.ucase.casemain.enums.*;
import com.icinfo.cloud.provider.punish.ucase.casemain.mapper.CaseMainDetailAuxiliaryInfoMapper;
import com.icinfo.cloud.provider.punish.ucase.casemain.mapper.CasePartiesLegalInfoMapper;
import com.icinfo.cloud.provider.punish.ucase.casemain.mapper.CasePartiesPersonInfoMapper;
import com.icinfo.cloud.provider.punish.ucase.casemain.mapper.CasePartiesRecordMapper;
import com.icinfo.cloud.provider.punish.ucase.casemain.model.*;
import com.icinfo.cloud.provider.punish.ucase.casemain.service.ICaseDocumentRecordService;
import com.icinfo.cloud.provider.punish.ucase.casemain.service.ICaseMainDetailInfoService;
import com.icinfo.cloud.provider.punish.ucase.casemain.service.ICaseMainInfoService;
import com.icinfo.cloud.provider.punish.ucase.casemain.service.ICasePartiesRecordService;
import com.icinfo.cloud.provider.punish.ucase.casemain.vo.*;
import com.icinfo.cloud.provider.punish.ucase.caseregister.dto.CaseRegisterPartiesPersonInfoDto;
import com.icinfo.cloud.provider.punish.ucase.caseregister.vo.CaseRegisterInfoVo;
import com.icinfo.cloud.provider.punish.ucase.investigateevidence.mapper.CaseDocumentAuthedEntrustMapper;
import com.icinfo.cloud.provider.punish.ucase.investigateevidence.model.CaseDocumentAuthedEntrust;
import com.icinfo.cloud.provider.punish.ucase.investigateevidence.vo.CasePartyMsgVo;
import com.icinfo.framework.common.web.Result;
import com.icinfo.framework.core.service.support.MyBatisServiceSupport;
import com.icinfo.framework.mybatis.mapper.entity.Example;
import com.icinfo.framework.tools.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;


/**
 * 描述: 案件当事人表-TRJ case_parties_info 对应的Service接口实现类.<br>
 *
 * @author framework generator
 * @date 2022年02月15日
 */
@Service
@Slf4j
public class CasePartiesRecordServiceImpl extends MyBatisServiceSupport implements ICasePartiesRecordService {

    @Resource
    private CasePartiesRecordMapper casePartiesRecordMapper;
    @Resource
    private CasePartiesPersonInfoMapper casePartiesPersonInfoMapper;
    @Resource
    private CasePartiesLegalInfoMapper casePartiesLegalnfoMapper;
    @Resource
    private CaseDocumentAuthedEntrustMapper caseDocumentAuthedEntrustMapper;
    @Resource
    private ICaseMainInfoService caseMainInfoService;
    @Resource
    private ICaseDocumentRecordService caseDocumentRecordService;
    @Resource
    private ICaseMainDetailInfoService caseMainDetailInfoService;
    @Resource
    private IConfigBusiParamDocumentInfoService configBusiParamDocumentInfoService;
    @Resource
    private CaseMainDetailAuxiliaryInfoMapper caseMainDetailAuxiliaryInfoMapper;
    @Resource
    private CaseSourcePartyInfoMapper caseSourcePartyInfoMapper;
    /**
     * 【V1.0.0】描述：保存案件当事人信息
     *
     * @param saveDto {@link CasePartiesRecordDto}
     * @return {@link Result <String>} 保存结果
     * @author xiangyuyu
     * @date 2022-02-18 16:02
     */
    @Override
    public int saveCasePartiesInfo(CasePartiesRecordDto saveDto) {
        String errorMsg = validatePartiesInfo(saveDto);
        if (!ObjectUtils.isEmpty(errorMsg)) {
            throw new BusinessException(errorMsg);
        }
        CasePartiesRecord casePartiesRecord = new CasePartiesRecord();
        BeanUtils.copyProperties(saveDto, casePartiesRecord);
        int count = casePartiesRecordMapper.insertSelective(casePartiesRecord);
        return count;
    }

    /**
     * 描述：校验当事人信息
     *
     * @param dto {@link CasePartiesRecordDto}
     * @return String 错误信息
     * @author xiangyuyu
     * @date 2022-06-20 11:46
     */
    private String validatePartiesInfo(CasePartiesRecordDto dto) {
        String partyType = dto.getPartyType();
        if (!PartyTypeEnum.isLegalEnumCode(partyType)) {
            return "请指定有效的当事人类型";
        }
        if (PartyTypeEnum.PERSON.getCode().equals(partyType)) {
            if (ObjectUtils.isEmpty(dto.getPartyIdentityType())) {
                return "请指定当事人身份证件类型";
            }
        }
        if (ObjectUtils.isEmpty(dto.getCaseId())) {
            return "请指定案件id";
        }
        if (ObjectUtils.isEmpty(dto.getPartyName())) {
            return "请指定当事人名称";
        }
        if (ObjectUtils.isEmpty(dto.getPartyIdentityCode())) {
            return "请指定当事人身份编码";
        }
        if (ObjectUtils.isEmpty(dto.getPartyId())) {
            return "请指定当事人id";
        }

        return null;
    }

    /**
     * 【V1.0.0】描述：获取案件当事人信息分页列表
     *
     * @param pageRequest {@link CasePartiesRecordPageRequest}
     * @return {@link Result< List <  CasePartiesRecordVo  >>} 案件当事人信息分页列表
     * @author xiangyuyu
     * @date 2022-02-18 16:02
     */
    @Override
    public List<CasePartiesRecordVo> getCasePartiesInfoPageList(CasePartiesRecordPageRequest pageRequest) {
        return null;
    }

    /**
     * 【V2.0.0】描述：获取案件当事人信息列表
     *
     * @param queryDto {@link CasePartiesRecordQueryDto}
     * @return {@link List< CasePartiesInfoVo >} 案件当事人信息列表
     * @author xiangyuyu
     * @date 2022-02-18 16:02
     */
    @Override
    public List<CasePartiesInfoVo> getCasePartiesInfoList(CasePartiesRecordQueryDto queryDto) {
        String caseId = queryDto.getCaseId();
        CaseMainInfoVo caseMainInfo = caseMainInfoService.getCaseMainInfo(caseId);
        List<CasePartiesInfoVo> casePartiesInfoList=new ArrayList<>();
        //案源不予立案
        if(CaseStatusEnum.PUNISH_NO_REGISTER.getCode().equals(caseMainInfo.getCaseStatus())&&ApiVisitDataSourceEnum.PUNISH_AY.getCode().equals(caseMainInfo.getDataSource())){
            casePartiesInfoList=getCaseSourcePartiesInfoList(caseMainInfo);
            return casePartiesInfoList;
        }
        if(ObjectUtils.isEmpty(queryDto.getIsQueryInvalidData())){
            queryDto.setIsQueryInvalidData(YesOrNoEnum.NO.getCode());
        }
        casePartiesInfoList = casePartiesRecordMapper.selectCasePartiesInfoList(queryDto);
        if (ObjectUtils.isEmpty(casePartiesInfoList) || casePartiesInfoList.size() == 0) {
            throw new BusinessException("未匹配到当事人信息 caseId: " + caseId);
        }
        casePartiesInfoList.forEach(e -> {
            String certType = e.getPartyIdentityType();
            String certCode = e.getPartyIdentityCode();
            String partyType = e.getPartyType();
            e.setPartyTypeName(PartyTypeEnum.getValueByCode(partyType));
            String legalCertNo = e.getLegalCertNo();
            if (PartyTypeEnum.LEGAL.getCode().equals(partyType)) {
                e.setPartyIdentityType("统一社会信用代码");
                if (!ObjectUtils.isEmpty(legalCertNo)) {
                    //法定代表人/负责人证件号码脱敏
                    //e.setLegalCertNo(BaseUtil.desensitizedIdNumber(legalCertNo));
                    //移动端调用不转换法人证件类型
                    if (!YesOrNoEnum.YES.getCode().equals(queryDto.getIsApp())) {
                        e.setLegalCertType(CertTypeEnum.getValueByCode(e.getLegalCertType()));
                    }
                    e.setEntTypeName(EnterTypeEnum.getValueByCode(e.getEntType()));
                }
            } else {
                if (YesOrNoEnum.YES.getCode().equals(queryDto.getIsApp())
                        && CertTypeEnum.SFZ.getCode().equals(e.getPartyIdentityType())) {
                    //移动端-设置个人身份证脱敏
                    e.setPartyIdentityCode(BaseUtil.desensitizedIdNumber(e.getPartyIdentityCode()));
                }
                String partyIdentityType = e.getPartyIdentityType();
                e.setPartyIdentityType(CertTypeEnum.getValueByCode(partyIdentityType));
                e.setPartyIdentityTypeCode(partyIdentityType);

                String caseType = caseMainInfo.getCaseType();
                String caseStatus = caseMainInfo.getCaseStatus();
                if(CaseTypeEnum.NORMAL_PROCEDURE.getCode().equals(caseType) && !CaseStatusEnum.TEMP.getCode().equals(caseStatus)){
                    //个人住址回显
                    //先从当事人记录表中查询
                    String addressDetail = e.getAddressDetail();
                    String addressAreasStr = e.getAddressAreasStr();

                    if(!ObjectUtils.isEmpty(addressDetail) && !ObjectUtils.isEmpty(addressAreasStr)){
                        String[] addressAreas = addressAreasStr.split(Constant.STRING_COMMA);
                        e.setAddressAreas(addressAreas);
                        e.setAddressDetail(addressDetail);
                    } else {
                        //获取立案登记表中的content内容
                        String kindCode= DocumentKindEnum.DOC_KIND_LASPB.getCode();
                        String documentCatalogCode;
                        List<ConfigBusiParamDocumentInfo> configList = configBusiParamDocumentInfoService.getDocumentConfigInfoByDocumentKindCode(caseId,caseMainInfo,kindCode);
                        if(!ObjectUtils.isEmpty(configList)){
                            documentCatalogCode = configList.get(Constant.INTEGER_0).getDocumentCatalogCode();
                        } else {
                            throw new BusinessException("未匹配文书目录编码信息-caseId:" + caseId + ",kindCode:" + kindCode);
                        }
                        CaseDocumentRecord caseDocumentRecord = caseDocumentRecordService.getCaseDocumentRecord(caseId,documentCatalogCode);
                        if(!ObjectUtils.isEmpty(caseDocumentRecord)){
                            String content = caseDocumentRecord.getDocumentContent();
                            if (!ObjectUtils.isEmpty(content)) {
                                JSONObject jsonObj = JSONObject.parseObject(content);
                                CaseRegisterInfoVo registerInfoVo = JSONObject.toJavaObject(jsonObj, CaseRegisterInfoVo.class);
                                List<CaseRegisterPartiesPersonInfoDto> partiesPersonInfoList = registerInfoVo.getPartiesPersonInfoList();
                                if (!ObjectUtils.isEmpty(partiesPersonInfoList) && partiesPersonInfoList.size() > 0) {
                                    partiesPersonInfoList.forEach(ele -> {
                                        String tmpCertCode = ele.getCertNo();
                                        String tmpCertType = ele.getCertType();
                                        if (tmpCertType.equals(certType) && tmpCertCode.equals(certCode)) {
                                            String[] addressAreas = ele.getAddressAreas();
                                            String detailAddress = ele.getAddressDetail();
                                            e.setAddressAreas(addressAreas);
                                            e.setAddressDetail(detailAddress);

                                        }
                                    });
                                }
                            }
                        }

                    }
                }
            }
        });
        return casePartiesInfoList;
    }
    /**
     * 获取案源当事人信息
     * @author liyafeng
     * @date 2022/8/19
     * @param caseMainInfo
     * @return {@link List< CasePartiesInfoVo>}
     */
    private List<CasePartiesInfoVo> getCaseSourcePartiesInfoList(CaseMainInfoVo caseMainInfo) {
        List<CasePartiesInfoVo> casePartiesInfoVos=new ArrayList<>();
        CasePartiesInfoVo vo=new CasePartiesInfoVo();
        Example example=new Example(CaseSourcePartyInfo.class);
        example.createCriteria().andEqualTo("caseSourceId",caseMainInfo.getId()).andEqualTo("status",Constant.STRING_1);
        List<CaseSourcePartyInfo> caseSourcePartyInfos=caseSourcePartyInfoMapper.selectByExample(example);
        if(null!=caseSourcePartyInfos&&caseSourcePartyInfos.size()>0){
            caseSourcePartyInfos.forEach(caseSourcePartyInfo -> {
                BeanUtils.copyProperties(caseSourcePartyInfo,vo);
                vo.setPartyId(caseSourcePartyInfo.getId());
                vo.setAddress(caseSourcePartyInfo.getAddress());
                vo.setPartyIdentityCode(caseSourcePartyInfo.getPartyIdentityCode());
                vo.setBirth(caseSourcePartyInfo.getPersonBirth());
                vo.setSex(caseSourcePartyInfo.getPersonSex());
                vo.setLegalName(caseSourcePartyInfo.getLegalName());
                vo.setPartyName(caseSourcePartyInfo.getPartyName());
                vo.setLegalCertNo(caseSourcePartyInfo.getLegalCertNo());
                vo.setPartyType(caseSourcePartyInfo.getPartyType());
                vo.setAddressAreas(caseSourcePartyInfo.getAddressAreas().split(","));
                vo.setAddressDetail(caseSourcePartyInfo.getAddressDetail());
                vo.setPartyIdentityType(caseSourcePartyInfo.getPartyIdentityType());
                vo.setWorkUnitName(caseSourcePartyInfo.getPersonUnitName());
                vo.setNationality(caseSourcePartyInfo.getPersonNationality());
                vo.setJob(caseSourcePartyInfo.getJob());
                vo.setContactPhone(caseSourcePartyInfo.getMobile());
                vo.setBusinessAddress(caseSourcePartyInfo.getEntBusinessAddress());
                vo.setAge(caseSourcePartyInfo.getPersonAge());
                vo.setPartyTypeName(caseSourcePartyInfo.getPartyType());
                vo.setPartyIdentityTypeCode(caseSourcePartyInfo.getPartyIdentityCode());
                vo.setLegalCertType(caseSourcePartyInfo.getLegalCertType());
                vo.setEntType(caseSourcePartyInfo.getEntType());
                casePartiesInfoVos.add(vo);
            });
        }
        return  casePartiesInfoVos;

    }

    /**
     * 描述：获取当事人id、名称信息
     *
     * @param caseId 案件id
     * @return Map<String, String>  当事人id、名称信息（多当事人时id、name逗号分隔）
     * 返回示例：{partyId:"1001",partyName:"张三"}
     * @author xiangyuyu
     * @date 2022-05-26 15:49
     */
    @Override
    public Map<String, String> getCasePartyIdNameInfo(String caseId) {
        String partyId = "";
        String partyName = "";
        StringBuilder tmpIdStr = new StringBuilder();
        StringBuilder tmpNameStr = new StringBuilder();
        List<CasePartiesRecordVo> casePartiesRecordList = getCasePartiesInfo(caseId);
        casePartiesRecordList.forEach(e -> {
            tmpIdStr.append(e.getPartyId() + ",");
            tmpNameStr.append(e.getPartyName() + ",");
        });

        if (tmpIdStr.length() > 0) {
            partyId = tmpIdStr.substring(0, tmpIdStr.length() - 1);
        }
        if (tmpNameStr.length() > 0) {
            partyName = tmpNameStr.substring(0, tmpNameStr.length() - 1);
        }
        Map<String, String> map = new HashMap<>(2);
        map.put("partyId", partyId);
        map.put("partyName", partyName);
        return map;
    }

    /**
     * 【V1.0.0】描述：根据案件id查询案件当事人信息
     *
     * @param caseId 案件id
     * @return {@link CasePartiesRecordVo} 案件当事人信息
     * @author xiangyuyu
     * @date 2022-02-18 16:02
     */
    @Override
    public List<CasePartiesRecordVo> getCasePartiesInfo(String caseId) {
        log.info("getCasePartiesInfo start(caseId:+" + caseId + ")...");
        Example queryExample = new Example(CasePartiesRecord.class);
        queryExample.createCriteria().andEqualTo("caseId", caseId).andEqualTo("status",Constant.STRING_1);
        queryExample.setOrderByClause(" party_type desc,create_time asc ");
        List<CasePartiesRecord> casePartiesRecord = casePartiesRecordMapper.selectByExample(queryExample);
        if (ObjectUtils.isEmpty(casePartiesRecord) || casePartiesRecord.size() == 0) {
            throw new BusinessException("未找到当事人信息");
        } else {
            List<CasePartiesRecordVo> parties = new ArrayList<>();
            casePartiesRecord.forEach(e -> {
                CasePartiesRecordVo party = new CasePartiesRecordVo();
                BeanUtils.copyProperties(e, party);
                parties.add(party);
            });
            log.info("getCasePartiesInfo end-parties:" + parties);
            return parties;
        }
    }

    /**
     * 【V2.0.0】描述：根据案件id查询案件展示的第一个当事人信息
     *
     * @param caseId 案件id
     * @return {@link List< CasePartiesRecordVo >} 案件当事人信息
     * @author xiangyuyu
     * @date 2022-02-18 16:02
     */
    @Override
    public CasePartiesRecordVo getFirstShowCasePartiesInfo(String caseId) {
        List<CasePartiesRecordVo> list = getCasePartiesInfo(caseId);
        CasePartiesRecordVo firstShow = list.get(Constant.INTEGER_0);
        return firstShow;
    }

    /**
     * 【V1.0.0】描述：删除案件当事人信息
     *
     * @param id 主键id
     * @return {@link Result<String>} 删除结果
     * @author xiangyuyu
     * @date 2022-02-18 16:02
     */
    @Override
    public Result<String> removeCasePartiesInfo(String id) {
        return null;
    }

    /**
     * 【V1.0.0】描述：根据案件id查询案件当事人详细信息（具体返回个人/法人信息）
     *
     * @param caseId 案件id
     * @return {@link CasePartiesDetailInfoVo} 案件当事人详细信息（具体返回个人/法人信息）
     * @author xiangyuyu
     * @date 2022-02-22 10:57
     */
    @Override
    public CasePartiesDetailInfoVo getCasePartiesDetailInfo(String caseId) {
        List<CasePartiesRecordVo> casePartiesInfo = getCasePartiesInfo(caseId);
        if (ObjectUtils.isEmpty(casePartiesInfo)) {
            throw new BusinessException("未找到当事人信息");
        }
        //目前只考虑1个当事人
        CasePartiesRecordVo party = casePartiesInfo.get(Constant.INTEGER_0);
        String partyType = party.getPartyType();
        CasePartiesDetailInfoVo vo = new CasePartiesDetailInfoVo();
        String partyId = party.getPartyId();
        if (PartyTypeEnum.PERSON.getCode().equals(partyType)) {
            CasePartiesPersonInfo casePartiesPersonInfo = casePartiesPersonInfoMapper.selectByPrimaryKey(partyId);
            if (ObjectUtils.isEmpty(casePartiesPersonInfo)) {
                throw new BusinessException("未找到当事人个人信息");
            }
            CasePartiesPersonInfoVo casePartiesPersonInfoVo = new CasePartiesPersonInfoVo();
            BeanUtils.copyProperties(casePartiesPersonInfo, casePartiesPersonInfoVo);
            // 证件类型
            for (CertTypeEnum cert : CertTypeEnum.values()) {
                if (cert.getCode().equals(casePartiesPersonInfoVo.getCertType())) {
                    casePartiesPersonInfoVo.setCertType(cert.getDes());
                }
            }
            vo.setCasePartiesPersonInfo(casePartiesPersonInfoVo);
        } else if (PartyTypeEnum.LEGAL.getCode().equals(partyType)) {
            CasePartiesLegalInfo casePartiesLegalInfo = casePartiesLegalnfoMapper.selectByPrimaryKey(partyId);
            if (ObjectUtils.isEmpty(casePartiesLegalInfo)) {
                throw new BusinessException("未找到当事人法人信息");
            }
            CasePartiesLegalInfoVo casePartiesLegalInfoVo = new CasePartiesLegalInfoVo();
            BeanUtils.copyProperties(casePartiesLegalInfo, casePartiesLegalInfoVo);
            vo.setCasePartiesLegalInfo(casePartiesLegalInfoVo);
        } else {
            throw new BusinessException("非法的当事人类型");
        }
        vo.setCaseId(caseId);
        vo.setPartyType(partyType);
        return vo;
    }


    /**
     * 根据案件ID和partyId获取当事人信息
     * @param caseId
     * @return
     */
    @Override
    public CasePartiesDetailInfoVo getCasePartiesDetailInfoByCaseIdAndPartyId(String caseId, String partyId){
        CasePartiesDetailInfoVo vo = new CasePartiesDetailInfoVo();
        //查询主表当事人信息
        Example queryExample = new Example(CasePartiesRecord.class);
        queryExample.createCriteria().andEqualTo("caseId", caseId)
                .andEqualTo("status",Constant.STRING_1)
                .andEqualTo("partyId", partyId);
        List<CasePartiesRecord> casePartiesRecord = casePartiesRecordMapper.selectByExample(queryExample);
        if (ObjectUtils.isEmpty(casePartiesRecord) || casePartiesRecord.size() == 0) {
            throw new BusinessException("未找到当事人信息");
        }
        CasePartiesRecord partyInfo = casePartiesRecord.get(Constant.INTEGER_0);
        String partyType = partyInfo.getPartyType();
        if (PartyTypeEnum.PERSON.getCode().equals(partyType)) {
            CasePartiesPersonInfo casePartiesPersonInfo = casePartiesPersonInfoMapper.selectByPrimaryKey(partyId);
            if (ObjectUtils.isEmpty(casePartiesPersonInfo)) {
                throw new BusinessException("未找到当事人个人信息");
            }
            CasePartiesPersonInfoVo casePartiesPersonInfoVo = new CasePartiesPersonInfoVo();
            BeanUtils.copyProperties(casePartiesPersonInfo, casePartiesPersonInfoVo);
            // 证件类型
            for (CertTypeEnum cert : CertTypeEnum.values()) {
                if (cert.getCode().equals(casePartiesPersonInfoVo.getCertType())) {
                    casePartiesPersonInfoVo.setCertType(cert.getDes());
                }
            }
            vo.setCasePartiesPersonInfo(casePartiesPersonInfoVo);
        } else if (PartyTypeEnum.LEGAL.getCode().equals(partyType)) {
            CasePartiesLegalInfo casePartiesLegalInfo = casePartiesLegalnfoMapper.selectByPrimaryKey(partyId);
            if (ObjectUtils.isEmpty(casePartiesLegalInfo)) {
                throw new BusinessException("未找到当事人法人信息");
            }
            CasePartiesLegalInfoVo casePartiesLegalInfoVo = new CasePartiesLegalInfoVo();
            BeanUtils.copyProperties(casePartiesLegalInfo, casePartiesLegalInfoVo);
            vo.setCasePartiesLegalInfo(casePartiesLegalInfoVo);
        } else {
            throw new BusinessException("非法的当事人类型");
        }
        vo.setCaseId(caseId);
        vo.setPartyType(partyType);
        return vo;
    }


    /**
     * 【V1.0.0】描述：根据案件id查询案件当事人详细信息（具体返回个人/法人信息）
     *
     * @param caseId 案件id
     * @return {@link CasePartiesDetailInfoVo} 案件当事人详细信息（具体返回个人/法人信息）
     * @author xiangyuyu
     * @date 2022-02-22 10:57
     */
    @Override
    public CasePartiesDetailInfoVo getCaseAdditionalPartiesDetailInfo(String caseId) {
        List<CasePartiesRecordVo> casePartiesInfo = getCasePartiesInfo(caseId);
        if (ObjectUtils.isEmpty(casePartiesInfo)) {
            throw new BusinessException("未找到当事人信息");
        }
        //目前只考虑1个当事人
        CasePartiesRecordVo party = casePartiesInfo.get(Constant.INTEGER_0);
        String partyType = party.getPartyType();
        CasePartiesDetailInfoVo vo = new CasePartiesDetailInfoVo();
        String partyId = party.getPartyId();
        if (PartyTypeEnum.PERSON.getCode().equals(partyType)) {
            CasePartiesPersonInfo casePartiesPersonInfo = casePartiesPersonInfoMapper.selectByPrimaryKey(partyId);
            if (ObjectUtils.isEmpty(casePartiesPersonInfo)) {
                throw new BusinessException("未找到当事人个人信息");
            }
            CasePartiesPersonInfoVo casePartiesPersonInfoVo = new CasePartiesPersonInfoVo();
            BeanUtils.copyProperties(casePartiesPersonInfo, casePartiesPersonInfoVo);
            // 证件类型
            for (CertTypeEnum cert : CertTypeEnum.values()) {
                if (cert.getCode().equals(casePartiesPersonInfoVo.getCertType())) {
                    casePartiesPersonInfoVo.setCertType(cert.getDes());
                }
            }
            vo.setCasePartiesPersonInfo(casePartiesPersonInfoVo);
        } else {
            CasePartiesLegalInfo casePartiesLegalInfo = casePartiesLegalnfoMapper.selectByPrimaryKey(partyId);
            if (ObjectUtils.isEmpty(casePartiesLegalInfo)) {
                throw new BusinessException("未找到当事人法人信息");
            }
            CasePartiesLegalInfoVo casePartiesLegalInfoVo = new CasePartiesLegalInfoVo();
            BeanUtils.copyProperties(casePartiesLegalInfo, casePartiesLegalInfoVo);
            vo.setCasePartiesLegalInfo(casePartiesLegalInfoVo);
        }
        vo.setCaseId(caseId);
        vo.setPartyType(partyType);
        return vo;
    }

    /**
     * 获取授权委托人+当事人信息
     *
     * @param caseId 案件id
     * @return List
     * @author lxj
     * @date 2022/3/2
     **/
    @Override
    public List<CasePartyMsgVo> getAuthorizedPersonAndPartyList(String caseId, String partyId) {
        //多当事人案件改造
        //1.查询受委托人表是否有委托人
        Example example = new Example(CaseDocumentAuthedEntrust.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("caseId", caseId).andEqualTo("status", Constant.INTEGER_1);

        CaseMainInfoVo caseMainInfo = caseMainInfoService.getCaseMainInfo(caseId);
        String casePartiesReasonType = caseMainInfo.getCasePartiesReasonType();
        //多当事人案件partyId不为空
        if (PartiesReasonTypeEnum.MULTIPLE_PARTIES.getCode().equals(casePartiesReasonType)) {
            if (ObjectUtils.isEmpty(partyId)) {
                throw new BusinessException("多当事人案件请指定当事人id");
            }
            criteria.andEqualTo("partyId", partyId);
        }

        List<CasePartyMsgVo> list = new ArrayList<>();
        List<CaseDocumentAuthedEntrust> caseDocumentAuthedEntrustList = caseDocumentAuthedEntrustMapper.selectByExample(example);
        if (!ObjectUtils.isEmpty(caseDocumentAuthedEntrustList)) {
            for (CaseDocumentAuthedEntrust entrust : caseDocumentAuthedEntrustList) {
                CasePartyMsgVo vo = new CasePartyMsgVo();
                vo.setPartyId(entrust.getPartyId());
                vo.setName(entrust.getAuthedName());
                vo.setPersonType(Constant.INTEGER_3);
                list.add(vo);
            }
        }
        //2.查询当事人信息
        Example partyExample = new Example(CasePartiesRecord.class);
        Example.Criteria partyCriteria = partyExample.createCriteria();
        partyCriteria.andEqualTo("caseId", caseId).andEqualTo("status",Constant.STRING_1);
        if (PartiesReasonTypeEnum.MULTIPLE_PARTIES.getCode().equals(casePartiesReasonType)) {
            partyCriteria.andEqualTo("partyId", partyId);
        }
        List<CasePartiesRecordVo> casePartiesRecordList = getCasePartiesInfo(caseId);
        if (!ObjectUtils.isEmpty(casePartiesRecordList)) {
            for (CasePartiesRecordVo casePartiesRecord : casePartiesRecordList) {
                if (PartyTypeEnum.PERSON.getCode().equals(casePartiesRecord.getPartyType())) {
                    CasePartiesPersonInfo personInfo = casePartiesPersonInfoMapper.selectByPrimaryKey(casePartiesRecord.getPartyId());
                    CasePartyMsgVo vo = new CasePartyMsgVo();
                    vo.setPartyId(personInfo.getId());
                    vo.setName(personInfo.getName());
                    vo.setPersonType(Constant.INTEGER_1);
                    list.add(vo);
                } else if (PartyTypeEnum.LEGAL.getCode().equals(casePartiesRecord.getPartyType())) {
                    CasePartiesLegalInfo legalInfo = casePartiesLegalnfoMapper.selectByPrimaryKey(casePartiesRecord.getPartyId());
                    CasePartyMsgVo vo = new CasePartyMsgVo();
                    vo.setPartyId(legalInfo.getId());
                    vo.setName(legalInfo.getLegalName());
                    vo.setPersonType(Constant.INTEGER_2);
                    list.add(vo);
                }
            }
        }
        return list;
    }

    @Override
    public CasePartiesRecord getCasePartiesRecordByCaseIdAndCertNo(CasePartiesRecordSingleQueryDto queryDto) {
        CasePartiesRecord casePartiesRecord = casePartiesRecordMapper.selectCasePartiesRecordByCaseIdAndCertNo(queryDto);
        return casePartiesRecord;
    }

    @Override
    public boolean delCasePartiesRecordByCaseId(String caseId) {
        Example queryExample = new Example(CasePartiesRecord.class);
        queryExample.createCriteria().andEqualTo("caseId", caseId).andEqualTo("status",Constant.STRING_1);;
        List<CasePartiesRecord> casePartiesRecordList = casePartiesRecordMapper.selectByExample(queryExample);
        if (ObjectUtil.isNotEmpty(casePartiesRecordList)) {
            for (CasePartiesRecord casePartiesRecord : casePartiesRecordList) {
                casePartiesRecordMapper.deleteByPrimaryKey(casePartiesRecord.getId());
                if (PartyTypeEnum.PERSON.getCode().equals(casePartiesRecord.getPartyType())) {
                    casePartiesPersonInfoMapper.deleteByPrimaryKey(casePartiesRecord.getPartyId());
                } else if (PartyTypeEnum.PERSON.getCode().equals(casePartiesRecord.getPartyType())) {
                    casePartiesLegalnfoMapper.deleteByPrimaryKey(casePartiesRecord.getPartyId());
                }
            }
        }
        return true;
    }

    /**
     * 【V1.0.0】描述：根据案件id查询案件当事人详细信息（具体返回个人/法人信息）
     *
     * @param queryDto 查询dto
     * @return {@link List<Map<String, Object>> }
     * @author shishengyao
     * @date 2022/05/26
     */
    @Override
    public List<Map<String, Object>> getCasePartiesDetailInfoList(CasePartiesQueryDto queryDto) {
        String caseId = queryDto.getCaseId();
        CaseMainInfoVo caseMainInfo = caseMainInfoService.getCaseMainInfo(caseId);
        String partyId =queryDto.getPartyId();
        List<Map<String, Object>> partiesList = new ArrayList<>();
        List<CasePartiesRecordVo> casePartiesInfos =new ArrayList<>();
        ConfigBusiParamDocumentInfo configBusiParamDocumentInfo=new ConfigBusiParamDocumentInfo();
        if(StringUtils.isNotBlank(queryDto.getDocumentCatalogCode())){
            configBusiParamDocumentInfo=configBusiParamDocumentInfoService.getDetailByDocumentCatalogCode(queryDto.getDocumentCatalogCode());
        }
        if(StringUtils.isBlank(caseMainInfo.getHandleType())&&Constant.STRING_2.equals(configBusiParamDocumentInfo.getDocumentLinkObjectType())&& !Constant.STRING_1.equals(queryDto.getDocumentPartyType())){
            CasePartiesRecordVo partiesRecordVo=getCasePartiesInfoByPartyId(caseId,partyId);
            casePartiesInfos.add(partiesRecordVo);
        } else if (HandleTypeEnum.PART_PUNISH.getCode().equals(caseMainInfo.getHandleType())
                && !Constant.STRING_1.equals(queryDto.getDocumentPartyType())) {
            if (ObjectUtil.isEmpty(queryDto.getPartyId())) {
                throw new BusinessException("分别处罚当事人id不能为空");
            }
            CasePartiesRecordVo partiesRecordVo=getCasePartiesInfoByPartyId(caseId,partyId);
            casePartiesInfos.add(partiesRecordVo);
        } else {
            casePartiesInfos = getCasePartiesInfo(queryDto.getCaseId());
        }
        String partyType= StringUtils.isEmpty(queryDto.getPartyType())?"":queryDto.getPartyType();
        if (ObjectUtil.isNotEmpty(casePartiesInfos) && casePartiesInfos.size() > 0) {
            for (CasePartiesRecordVo casePartiesRecordVo : casePartiesInfos) {
                String  casePartiesType=   casePartiesRecordVo.getPartyType();
                //个人信息
                if (PartyTypeEnum.PERSON.getCode().equals(casePartiesType)
                        && partyType.contains(casePartiesType)) {
                    Map<String, Object> paramMap = new HashMap<>();
                    CasePartiesPersonInfo personInfo = casePartiesPersonInfoMapper.selectByPrimaryKey(casePartiesRecordVo.getPartyId());
                    paramMap.put("partyType", casePartiesType);
                    paramMap.put("GR_ZJHM", personInfo.getCertNo());
                    paramMap.put("GR_CertType",CertTypeEnum.getValueByCode(personInfo.getCertType()));
                    paramMap.put("GR_LXDH", personInfo.getMobile());
                    paramMap.put("GR_XM", personInfo.getName());
                    paramMap.put("GR_CSNY",  DateUtil.formatDateStr(personInfo.getBirth()));
                    paramMap.put("GR_XB", personInfo.getSex());
                    paramMap.put("GR_ZZ", personInfo.getAddress());
                    paramMap.put("GR_AGE",personInfo.getAge());
                    paramMap.put("GR_GZDW", personInfo.getUnitName());
                    paramMap.put("GR_JOB",personInfo.getJob());
                    paramMap.put("GR_PostCode",personInfo.getPostCode());
                    paramMap.put("GR_UnitAddress",personInfo.getUnitAddress());
                    paramMap.put("GR_MZ", personInfo.getNationality());
                    paramMap.put("GR_ZHMC", "/");//字号名称
                    paramMap.put("GR_TYXYDM", "/");//统一社会信用代码
                    if (!ObjectUtils.isEmpty(personInfo.getUnitName())) {
                        paramMap.put("GR_GZHZW", personInfo.getUnitName());//工作单位和职务
                    } else {
                        paramMap.put("GR_GZHZW", "/");//工作单位和职务
                    }
                    partiesList.add(paramMap);
                }
                //


                //法人信息
                if ( PartyTypeEnum.LEGAL.getCode().equals(casePartiesType)) {
                    Map<String, Object> paramMap = new HashMap<>();
                    CasePartiesLegalInfo legalInfo = casePartiesLegalnfoMapper.selectByPrimaryKey(casePartiesRecordVo.getPartyId());
                    if (TerritoryCodeEnum.NYNC.getCode().equals(caseMainInfo.getCaseTerritoryCode())
                            && EnterTypeEnum.PRIVATELY_BUSINESS.getCode().equals(legalInfo.getEntType())) {
                        if (partyType.contains(PartyTypeEnum.PERSON.getCode())) {
                            //农业农村个体工商户信息放在个人列表中
                            paramMap.put("partyType", casePartiesType);
                            paramMap.put("GR_ZJHM", legalInfo.getLegalCertNo());
                            paramMap.put("GR_CertType",CertTypeEnum.getValueByCode(legalInfo.getLegalCertType()));
                            paramMap.put("GR_LXDH", legalInfo.getPhone());
                            paramMap.put("GR_XM", legalInfo.getLegalName());
                            paramMap.put("GR_CSNY",  "/");
                            paramMap.put("GR_XB", "/");
                            paramMap.put("GR_ZZ", legalInfo.getAddress());
                            paramMap.put("GR_AGE","/");
                            paramMap.put("GR_GZDW", legalInfo.getEntName());
                            paramMap.put("GR_JOB", legalInfo.getJob());
                            paramMap.put("GR_PostCode", legalInfo.getPostCode());
                            paramMap.put("GR_UnitAddress", legalInfo.getBusinessAddress());
                            paramMap.put("GR_MZ", "/");
                            paramMap.put("GR_ZHMC", legalInfo.getEntName());//字号名称
                            paramMap.put("GR_TYXYDM", legalInfo.getUniscid());//统一社会信用代码
                            paramMap.put("GR_GZHZW", legalInfo.getEntName() + " " + (ObjectUtils.isEmpty(legalInfo.getJob()) ? "" : legalInfo.getJob()));//工作单位和职务

                        }
                    } else if (partyType.contains(casePartiesType)){
                        paramMap.put("partyType", casePartiesType);
                        paramMap.put("FR_MC", legalInfo.getEntName());
                        paramMap.put("FR_ZS", legalInfo.getAddress());
                        paramMap.put("FR_ZW", legalInfo.getJob());
                        paramMap.put("FR_JOB", legalInfo.getJob());
                        paramMap.put("FR_TYXYDM", legalInfo.getUniscid());
                        paramMap.put("FR_LXDH", legalInfo.getPhone());
                        paramMap.put("FR_FDDBR", legalInfo.getLegalName());
                        paramMap.put("FR_legalCertType", CertTypeEnum.getValueByCode(legalInfo.getLegalCertType()));
                        paramMap.put("FR_legalCertNo", legalInfo.getLegalCertNo());
                        paramMap.put("FR_BusinessAddress", legalInfo.getBusinessAddress());
                        paramMap.put("FR_PostCode", legalInfo.getPostCode());
                    }
                    if (!paramMap.isEmpty()) {
                        partiesList.add(paramMap);
                    }
                }
            }
        }

        return partiesList;
    }
    /**
     * 根据案件id获取CasePartiesInfoVo
     * @author liyafeng
     * @date 2022/5/28
     * @param caseId
     * @return {@link List< CasePartiesInfoVo>}
     */
    @Override
    public List<CasePartiesInfoVo> getCasePartiesInfoVo(String caseId,String partyId) {
        CaseMainInfoVo caseMainInfo=caseMainInfoService.getCaseMainInfo(caseId);
        String handType=caseMainInfo.getHandleType();
        List<CasePartiesInfoVo> casePartiesInfoVos=new ArrayList<>();
        Example example=new Example(CasePartiesRecord.class);
        //  boolean handleTypeFlag=StringUtils.isEmpty(handType) || Constant.STRING_2.equals(handType);
        // partyId不为空并且不是合并当事人id,根据partyId查询当事人信息
        if(StringUtils.isNotBlank(partyId) && !partyId.contains(",")) {
            example.createCriteria().andEqualTo("caseId", caseId).andEqualTo("partyId", partyId).andEqualTo("status", Constant.STRING_1);
        }else{
            example.createCriteria().andEqualTo("caseId",caseId).andEqualTo("status",Constant.STRING_1);
        }
        example.setOrderByClause(" party_type desc,create_time asc ");
        List<CasePartiesRecord> casePartiesRecords=casePartiesRecordMapper.selectByExample(example);
        for (CasePartiesRecord casePartiesRecord:casePartiesRecords) {
            String partyType=casePartiesRecord.getPartyType();
            if(Constant.STRING_2.equals(partyType)){
                //法人
                CasePartiesLegalInfo casePartiesLegalInfo=casePartiesLegalnfoMapper.selectByPrimaryKey(casePartiesRecord.getPartyId());
                CasePartiesInfoVo casePartiesInfoVo=new CasePartiesInfoVo();
                BeanUtils.copyProperties(casePartiesLegalInfo,casePartiesInfoVo);
                casePartiesInfoVo.setPartyId(casePartiesLegalInfo.getId());
                casePartiesInfoVo.setPartyName(casePartiesLegalInfo.getEntName());
                casePartiesInfoVo.setPartyType(Constant.STRING_2);
                casePartiesInfoVo.setPartyIdentityCode(casePartiesLegalInfo.getUniscid());
                casePartiesInfoVo.setLegalName(casePartiesLegalInfo.getLegalName());
                casePartiesInfoVo.setAddress(casePartiesLegalInfo.getAddress());
                casePartiesInfoVos.add(casePartiesInfoVo);
            }else if(Constant.STRING_1.equals(partyType)){
                //个人
                CasePartiesPersonInfo casePartiesPersonInfo=casePartiesPersonInfoMapper.selectByPrimaryKey(casePartiesRecord.getPartyId());
                CasePartiesInfoVo casePartiesInfoVo=new CasePartiesInfoVo();
                BeanUtils.copyProperties(casePartiesPersonInfo,casePartiesInfoVo);
                casePartiesInfoVo.setPartyId(casePartiesPersonInfo.getId());
                casePartiesInfoVo.setPartyName(casePartiesPersonInfo.getName());
                casePartiesInfoVo.setPartyType(Constant.STRING_1);
                casePartiesInfoVo.setSex(casePartiesPersonInfo.getSex());
                casePartiesInfoVo.setBirth(casePartiesPersonInfo.getBirth());
                casePartiesInfoVo.setPartyIdentityCode(casePartiesPersonInfo.getCertNo());
                casePartiesInfoVo.setPartyIdentityType(casePartiesPersonInfo.getCertType());
                casePartiesInfoVo.setAddress(casePartiesPersonInfo.getAddress());
                casePartiesInfoVos.add(casePartiesInfoVo);
            }
        }


       /* //先查法人
        Example legalExample=new Example(CasePartiesLegalInfo.class);
        if(StringUtils.isBlank(partyId)){
            legalExample.createCriteria().andEqualTo("caseId",caseId);
        }else{
            legalExample.createCriteria().andEqualTo("caseId",caseId).andEqualTo("id",partyId);
        }

        List<CasePartiesLegalInfo> casePartiesLegalInfos=casePartiesLegalnfoMapper.selectByExample(legalExample);
        if(null!=casePartiesLegalInfos&&casePartiesLegalInfos.size()>0){
            for (CasePartiesLegalInfo casePartiesLegalInfo:casePartiesLegalInfos) {
                CasePartiesInfoVo casePartiesInfoVo=new CasePartiesInfoVo();
                casePartiesInfoVo.setPartyName(casePartiesLegalInfo.getEntName());
                casePartiesInfoVo.setPartyType(Constant.STRING_2);
                casePartiesInfoVo.setPartyIdentityCode(casePartiesLegalInfo.getUniscid());
                casePartiesInfoVo.setLegalName(casePartiesLegalInfo.getLegalName());
                casePartiesInfoVo.setAddress(casePartiesLegalInfo.getAddress());
                casePartiesInfoVos.add(casePartiesInfoVo);
            }
        }
        //再查个人
        Example personExample=new Example(CasePartiesPersonInfo.class);
        if(StringUtils.isBlank(partyId)){
            personExample.createCriteria().andEqualTo("caseId",caseId);
        }else{
            personExample.createCriteria().andEqualTo("caseId",caseId).andEqualTo("id",partyId);
        }
        List<CasePartiesPersonInfo> casePartiesPersonInfos=casePartiesPersonInfoMapper.selectByExample(legalExample);
        if(null!=casePartiesPersonInfos&&casePartiesPersonInfos.size()>0){
            for (CasePartiesPersonInfo casePartiesPersonInfo:casePartiesPersonInfos) {
                CasePartiesInfoVo casePartiesInfoVo=new CasePartiesInfoVo();
                casePartiesInfoVo.setPartyName(casePartiesPersonInfo.getName());
                casePartiesInfoVo.setPartyType(Constant.STRING_1);
                casePartiesInfoVo.setSex(casePartiesPersonInfo.getSex());
                casePartiesInfoVo.setBirth(casePartiesPersonInfo.getBirth());
                casePartiesInfoVo.setPartyIdentityCode(casePartiesPersonInfo.getCertNo());
                casePartiesInfoVo.setAddress(casePartiesPersonInfo.getAddress());
                casePartiesInfoVos.add(casePartiesInfoVo);
            }
        }*/
        return casePartiesInfoVos;
    }

    @Override
    public CasePartiesRecord getCasePartiesRecordByIdentityCode(String caseId , String partyIdentityCode, String partyType, String partyIdentityType) {
        Example example = new Example(CasePartiesRecord.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("caseId",caseId)
                .andEqualTo("partyIdentityCode", partyIdentityCode)
                .andEqualTo("partyType", partyType)
                .andEqualTo("status",Constant.STRING_1);
        if (ObjectUtil.isNotEmpty(partyIdentityType)) {
            criteria.andEqualTo("partyIdentityType", partyIdentityType);
        }
        List<CasePartiesRecord> partiesRecords = casePartiesRecordMapper.selectByExample(example);
        if (ObjectUtil.isNotEmpty(partiesRecords)) {
            return partiesRecords.get(0);
        }

        return null;
    }

    @Override
    public List<CasePartiesRecordVo> getCasePartiesInfoVoList(String caseId) {
        log.info("getCasePartiesInfoVoList start(caseId:+" + caseId + ")...");
        Example queryExample = new Example(CasePartiesRecord.class);
        queryExample.createCriteria().andEqualTo("caseId", caseId).andEqualTo("status",Constant.STRING_1);
        queryExample.setOrderByClause(" party_type desc,create_time asc ");
        List<CasePartiesRecord> casePartiesRecord = casePartiesRecordMapper.selectByExample(queryExample);
        List<CasePartiesRecordVo> parties = new ArrayList<>();
        if (ObjectUtil.isNotEmpty(casePartiesRecord) || casePartiesRecord.size() > 0) {
            casePartiesRecord.forEach(e -> {
                CasePartiesRecordVo party = new CasePartiesRecordVo();
                BeanUtils.copyProperties(e, party);
                parties.add(party);
            });
            log.info("getCasePartiesInfoVoList end-parties:" + parties);
        }
        return parties;
    }
    /**
     * 获取当事人信息(案件详情-新)
     * @author liyafeng
     * @date 2022/8/3
     * @param caseId
     * @return {@link List< CaseDetailPartiesInfoVo>}
     */
    @Override
    public List<CaseDetailPartiesInfoVo> getCaseDetailPartyInfo(String caseId) {
        CaseMainInfoVo caseMainInfoVo=caseMainInfoService.getCaseMainInfo(caseId);
        CaseMainDetailInfo caseMainDetailInfo=caseMainDetailInfoService.getCaseMainDetailByCaseId(caseId);
        List<CaseDetailPartiesInfoVo> caseDetailPartiesInfoVos=new ArrayList<>();
        Example example=new Example(CasePartiesRecord.class);
        example.createCriteria().andEqualTo("caseId",caseId).andEqualTo("status",Constant.STRING_1);
        example.orderBy("party_type desc");
        List<CasePartiesRecord> casePartiesRecords=casePartiesRecordMapper.selectByExample(example);
        if(null!=casePartiesRecords&&casePartiesRecords.size()>0){
            for (CasePartiesRecord casePartiesRecord:casePartiesRecords) {
                CaseDetailPartiesInfoVo caseDetailPartiesInfoVo=new CaseDetailPartiesInfoVo();
                caseDetailPartiesInfoVo.setPartyId(casePartiesRecord.getPartyId());
                caseDetailPartiesInfoVo.setBranchLinkName(caseMainInfoVo.getCaseCurrentLinkName());
                caseDetailPartiesInfoVo.setPartyName(casePartiesRecord.getPartyName());
                if(Constant.STRING_2.equals(casePartiesRecord.getPartyType())){
                    CasePartiesLegalInfo casePartiesLegalInfo=casePartiesLegalnfoMapper.selectByPrimaryKey(casePartiesRecord.getPartyId());
                    caseDetailPartiesInfoVo.setPartyLegalType(casePartiesLegalInfo.getEntType());
                }
                if(Constant.STRING_2.equals(caseMainInfoVo.getHandleType())){
                    Example auxiliaryExample=new Example(CaseMainDetailAuxiliaryInfo.class);
                    auxiliaryExample.createCriteria().andEqualTo("caseId",caseId).andEqualTo("partyId",casePartiesRecord.getPartyId());
                    List<CaseMainDetailAuxiliaryInfo> caseMainDetailAuxiliaryInfos=caseMainDetailAuxiliaryInfoMapper.selectByExample(auxiliaryExample);
                    if(null!=caseMainDetailAuxiliaryInfos&&caseMainDetailAuxiliaryInfos.size()>0){
                        caseDetailPartiesInfoVo.setPunishExplanation(caseMainDetailAuxiliaryInfos.get(0).getPunishContent());
                        caseDetailPartiesInfoVo.setDisposeContext(caseMainDetailAuxiliaryInfos.get(0).getDisposeContext());
                    }
                }else{
                    if(Constant.STRING_2.equals(caseMainDetailInfo.getHandleConclusion())){
                        Example auxiliaryExample=new Example(CaseMainDetailAuxiliaryInfo.class);
                        auxiliaryExample.createCriteria().andEqualTo("caseId",caseId).andEqualTo("partyId",casePartiesRecord.getPartyId());
                        List<CaseMainDetailAuxiliaryInfo> caseMainDetailAuxiliaryInfos=caseMainDetailAuxiliaryInfoMapper.selectByExample(auxiliaryExample);
                        if(null!=caseMainDetailAuxiliaryInfos&&caseMainDetailAuxiliaryInfos.size()>0){
                            caseDetailPartiesInfoVo.setDisposeContext(caseMainDetailAuxiliaryInfos.get(0).getDisposeContext());
                        }
                    }
                    caseDetailPartiesInfoVo.setPunishExplanation(caseMainDetailInfo.getPunishContent());
                }
                Example authExample=new Example(CaseDocumentAuthedEntrust.class);
                authExample.createCriteria().andEqualTo("caseId",caseId).andEqualTo("status",Constant.INTEGER_1).andEqualTo("partyId", casePartiesRecord.getPartyId());
                List<CaseDocumentAuthedEntrust> caseDocumentAuthedEntrusts=caseDocumentAuthedEntrustMapper.selectByExample(authExample);
                if(null!=caseDocumentAuthedEntrusts&&caseDocumentAuthedEntrusts.size()>0){
                    caseDetailPartiesInfoVo.setConsignorName(caseDocumentAuthedEntrusts.get(0).getAuthedName());
                    CaseDocumentRecord caseDocumentRecord=new CaseDocumentRecord();
                    String docmentCatalogCode="";
                    String kindCode="SQWTS";
                    List<ConfigBusiParamDocumentInfo> configBusiParamDocumentInfos=configBusiParamDocumentInfoService.getDocumentConfigInfoByDocumentKindCode(caseId,caseMainInfoVo,kindCode);
                    if(null!=configBusiParamDocumentInfos&&configBusiParamDocumentInfos.size()>0){
                        docmentCatalogCode=configBusiParamDocumentInfos.get(0).getDocumentCatalogCode();
                        if(Constant.STRING_2.equals(caseMainInfoVo.getHandleType())){
                            caseDocumentRecord=caseDocumentRecordService.getCaseDocumentRecord(caseId,docmentCatalogCode,casePartiesRecord.getPartyId());
                        }else {
                            caseDocumentRecord=caseDocumentRecordService.getCaseDocumentRecord(caseId,docmentCatalogCode);
                        }
                    }
                    if (!ObjectUtils.isEmpty(caseDocumentRecord)) {
                        caseDetailPartiesInfoVo.setAuthedFileUrl(caseDocumentRecord.getDocumentUrl());
                    }
                }
                caseDetailPartiesInfoVos.add(caseDetailPartiesInfoVo);
            }
        }
        return caseDetailPartiesInfoVos;
    }

    @Override
    public CasePartiesRecordVo getCasePartiesInfoByPartyId(String caseId, String partyId) {
        log.info("getCasePartiesInfoByPartyId start(caseId:+" + caseId + ")...");
        Example queryExample = new Example(CasePartiesRecord.class);
        Example.Criteria criteria = queryExample.createCriteria();
        if (StrUtil.isNotEmpty(caseId)) {
            criteria.andEqualTo("caseId", caseId);
        }
        criteria.andEqualTo("partyId", partyId)
                .andEqualTo("status",Constant.STRING_1);
        queryExample.setOrderByClause(" party_type desc,create_time asc ");
        List<CasePartiesRecord> casePartiesRecord = casePartiesRecordMapper.selectByExample(queryExample);
        CasePartiesRecordVo partiesRecordVo = new CasePartiesRecordVo();
        if (ObjectUtils.isEmpty(casePartiesRecord) || casePartiesRecord.size() == 0) {
            throw new BusinessException("未找到当事人信息");
        } else {
            BeanUtils.copyProperties(casePartiesRecord.get(Constant.INTEGER_0), partiesRecordVo);
        }
        return partiesRecordVo;
    }

    /**
     * 【1.9.4】描述：根据案件id集合查询当事人信息
     *
     * @param caseIds {@link Set <String>}
     * @return {@link Map<String, List<CasePartiesRecordVo>>}
     * @author cds
     * @date 2022/12/9
     * @version 1.9.4
     */
    @Override
    public Map<String, List<CasePartiesInfoVo>> listCasePartiesRecordByCaseIds(Set<String> caseIds) {
        List<CasePartiesInfoVo> records = casePartiesRecordMapper.listCasePartiesRecordByCaseIds(caseIds);
        if(CollUtil.isEmpty(records)){
            return CollUtil.newHashMap();
        }
        return records.stream().collect(Collectors.groupingBy(CasePartiesInfoVo::getCaseId));
    }

    /**
     * 获取当事人 姓名
     *
     * @param partyIds 当事人id，多个逗号分割
     * @return 当事人姓名，多个逗号 分割
     * @author lilihao
     * @date 2022/12/4 22:47
     */
    @Override
    public String getPartyNameByPartyIds(String partyIds) {
        Example example = new Example(CasePartiesRecord.class);
        example.createCriteria()
                .andIn("partyId", Arrays.asList(partyIds.split(",")));
        List<CasePartiesRecord> list = casePartiesRecordMapper.selectByExample(example);
        return CollectionUtil.isEmpty(list) ? "" : list.stream().map(CasePartiesRecord::getPartyName).collect(Collectors.joining(","));
    }

    @Override
    public String getPartyNamesByCaseId(String caseId) {
        Example example=new Example(CasePartiesRecord.class);
        example.createCriteria().andEqualTo("caseId",caseId);
        List<CasePartiesRecord> casePartiesRecords=casePartiesRecordMapper.selectByExample(example);
        String partyNames = "";
        if(null != casePartiesRecords) {
            int size = casePartiesRecords.size();
            CasePartiesRecord record = casePartiesRecords.get(Constant.INTEGER_0);
            if(size > 1){
                partyNames = record.getPartyName() + "等"+size+"人";
            }else{
                partyNames = record.getPartyName();
            }
        }
        return partyNames;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int delCasePartiesRecordByPartyId(String partyId) {
        Example updateExample = new Example(CasePartiesRecord.class);
        updateExample.createCriteria().andEqualTo("partyId", partyId);
        CasePartiesRecord casePartiesRecord=new CasePartiesRecord();
        casePartiesRecord.setStatus(Constant.STRING_0);
        return casePartiesRecordMapper.updateByExampleSelective(casePartiesRecord,updateExample);
    }
    @Override
    public List<CasePartyRecordAdjustPersonSaveDto> getCasePartiesLegalInfoDtoByCaseId(String caseId){
        List<CasePartyRecordAdjustPersonSaveDto> personSaveDtoList = casePartiesPersonInfoMapper.selectCasePartiesPersonInfoDtoByCaseId(caseId);
        personSaveDtoList.forEach(e->{
            String addressAreasStr = e.getAddressAreasStr();
            String addressDetail = e.getAddressDetail();
            if(!ObjectUtils.isEmpty(addressDetail) && !ObjectUtils.isEmpty(addressAreasStr)){
                String[] addressAreas = addressAreasStr.split(Constant.STRING_COMMA);
                e.setAddressAreas(addressAreas);
                e.setAddressDetail(addressDetail.replace(addressAreasStr.replace(",",""),""));
            }
        });
        return personSaveDtoList;
    }
    /**
     * 根据caseid获取partyId
     * @author liyafeng
     * @date 2023/3/21
      * @param caseId
     * @return {@link String}
     */
    @Override
    public String getPartIdsByCaseId(String caseId) {
        String partyId="";
        List<CasePartiesRecordVo> casePartiesRecordVos=getCasePartiesInfo(caseId);
        for (CasePartiesRecordVo e:casePartiesRecordVos) {
            if (StringUtils.isBlank(partyId)) {
                partyId = e.getPartyId();
            } else {
                partyId = partyId + "," + e.getPartyId();
            }
        }
        return partyId;
    }
}
